<?php
class Mcard extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	
	function getAppListforDownload($begin, $end) {
		$this->db->where ( "id >= $begin" );
		$this->db->where ( "id <= $end" );
		$query = $this->db->get ( 'cardapplication' );
		return $query->result_array ();
	}
	
	function balanceUpdate($str) {
		$this->db->truncate ( 'card' );
		return $this->db->query ( "LOAD DATA LOCAL INFILE '" . $str . "' INTO TABLE card FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n'; " );
	}
	
	function getCardAppMaxNum() {
		$this->db->select_max ( 'id', 'num' );
		$query = $this->db->get ( 'cardapplication' );
		$num = $query->row_array ();
		return $num ['num'];
	}
	
	function getCardAppNum($mode, $search) {
		if ($search) {
			$this->db->where ( $search );
		}
		if ($mode == 1)
			$this->db->where ( 'state', 'T' );
		if ($mode == 2)
			$this->db->where ( 'state', 'F' );
		if ($mode == 3)
			$this->db->where ( 'state', 'D' );
		return $this->db->count_all_results ( 'cardapplication' );
	}
	
	function getCardAppList($mode, $page, $num, $search) {
		if ($search) {
			$this->db->where ( $search );
		}
		if ($mode == 1)
			$this->db->where ( 'state', 'T' );
		if ($mode == 2)
			$this->db->where ( 'state', 'F' );
		if ($mode == 3)
			$this->db->where ( 'state', 'D' );
		$this->db->order_by ( "creatTime", "desc" );
		$this->db->limit ( $num, $page );
		$query = $this->db->get ( 'cardapplication' );
		return $query->result_array ();
	}
	
	function updateCardApp($data) {
		$this->db->where ( 'id', $data ['id'] );
		$this->db->update ( 'cardapplication', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function addCardApplication($data) {
		$data ['creatTime'] = date ( "Y/m/d H:i:s", time () );
		$this->db->insert ( 'cardapplication', $data );
		return $this->db->insert_id ();
	}
	
	function getBalancebyCardId($id) {
		$this->db->where ( 'card_id', $id );
		$query = $this->db->get ( 'card' );
		return $query->row_array ();
	}
	
	function getCardDetailsbyCardId($id) {
		$DBshop = $this->load->database ( 'shop', TRUE );
		$DBshop->select ( 'tbl_ns_settle_txn.card_id as cardId,tbl_ns_settle_txn.tx_amt58 as sum,tbl_ns_settle_txn.term_tx_date as date,tbl_ns_settle_txn.term_tx_time as time,tbl_ns_brnm.brnm_name as business' );
		$DBshop->where ( "tbl_ns_settle_txn.brnm_id = tbl_ns_brnm.brnm_id" );
		$DBshop->where ( "tbl_ns_settle_txn.card_id",$id );
		$DBshop->order_by("date", "desc");
		$DBshop->order_by("time", "desc");
		$query = $DBshop->get ( 'tbl_ns_settle_txn,tbl_ns_brnm' );
		return $query->result_array ();
		$DBshop->close ();
	}
	
	function addDetails($data) {
		$this->db->insert ( 'carddetails', $data );
		return $this->db->insert_id ();
	}
	
	function addBalance($data) {
		$this->db->insert ( 'card', $data );
		return $this->db->insert_id ();
	}
	
	function checkCardId($id) {
		$this->db->where ( 'card_id', $id );
		$query = $this->db->get ( 'card' );
		if ($query->num_rows () > 0)
			return true;
		else
			return FALSE;
	}
}